//
// Created by ct on 2020/10/26.
//
#ifndef NETTOP_UTILS_H
#define NETTOP_UTILS_H
#include <exception>
#include <cstring>
#include <sstream>
#include <ctime>

namespace nettop {
    class runtime_error : public std::exception {
        std::ostringstream      _oss;
        std::string             _str;
    public:
        explicit runtime_error(const char *e) noexcept{
            _oss << e;
            _str = _oss.str();
        }
        runtime_error(const runtime_error& rhs) noexcept {
            _oss.str(rhs._oss.str());
            _str = _oss.str();
        }
        runtime_error& operator=(const  runtime_error& rhs) noexcept {
            _oss.str(rhs._oss.str());
            _str = _oss.str();
            return *this;
        }

        template<typename T>
        runtime_error& operator<<(const T &in) {
            _oss << in;
            _str = _oss.str();
            return *this;
        }

        virtual const char* what() const throw() {
            return _str.c_str();
        }

        virtual ~runtime_error() throw() {
        }
    };
    inline double tv_to_sec(const timeval& tv) {
        return 1.0*tv.tv_sec + (1.0/1000000000.0)*tv.tv_usec;
    }
}
#endif //NETTOP_UTILS_H
